
/**
 * @params value
 * 数据过滤&lt &gt
 */
const whiteList = ['stdin', 'expectedOutput', 'template', 'codeCorrectValue', 'codeInput', 'codeOutput', 'codeInputExample', 'codeOutputExample', 'sourceCode']
export function responseTrim(Obj) {
  for (const i in Obj) {
    if (typeof Obj[i] === 'string') {
      if (whiteList.indexOf(i) < 0) {
        let str = Obj[i]
        str = str.replaceAll(/\&gt\;/g, '>')
        str = str.replaceAll(/\&lt\;/g, '<')
        Obj[i] = str
      }
    } else if (typeof Obj[i] === 'object') {
      responseTrim(Obj[i])
    }
  }
  return Obj
}

export function requestTrim(Obj) {
  for (const i in Obj) {
    if (typeof Obj[i] === 'string') {
      if (whiteList.indexOf(i) < 0) {
        let str = Obj[i]
        str = str.replaceAll(/\</g, '&lt;')
        str = str.replaceAll(/\>/g, '&gt;')
        Obj[i] = str
      }
    } else if (typeof Obj[i] === 'object') {
      requestTrim(Obj[i])
    }
  }
  return Obj
}

// 下载文件
export function downloadFile(blob, name) {
  const href = URL.createObjectURL(blob)
  const downloadElement = document.createElement('a')
  downloadElement.href = href
  downloadElement.download = name
  document.body.appendChild(downloadElement)
  downloadElement.click() // 点击下载
  document.body.removeChild(downloadElement) // 下载完成移除元素
  window.URL.revokeObjectURL(href)
}
